<?php
class MigrateController extends AppController {
	var $name = 'Migrate';
	var $uses = array();
	var $helpers = array('Javascript', 'Html');
	
	/**
	 @var $model
	 @brief Thông tin về model, được sử dụng trong trường hợp override thông tin của danh mục
	 */
	var $model = array(
		'plugin'	=>	'categories',
		'name'		=>	'Category'
	);
	/**
	 @var $model
	 @brief Thông tin về model, được sử dụng trong trường hợp override thông tin của danh mục
	 */
	var $models = array(
		'Category'	=>	array(
			'plugin'	=>	'categories',
			'name'		=>	'Category'
		)
	);
	
	function beforeFilter() {
		if (!Configure::read('migrating')) {
			exit('Not in migrating mode');
		}
		
		parent::beforeFilter();
		foreach ($this->models as $key => $item) {
			App::import('Model', $item['plugin'].".".$item['name']);
			$this->loadModel($item['name']);
		}
		$this->set('models', $this->models);
		
		$this->configs = $this->{$this->models['Page']['name']}->loadConfig();
		
		$this->category_root_id = $this->configs['Category']['root_id'];

		$this->params['named']['root'] = '';

		$this->set('configs', $this->configs);
	}
	
	function index() {
		//exit('Open exit function on line 48');
		$this->migrate_editable_pages();
		$this->migrate_related_pages();
		exit('finished');
	}
	function migrate_editable_pages() {
		//exit;
		App::import('Model', 'editable_pages.EditablePage');
		$this->loadModel('EditablePage');
		
		$info = $this->{$this->models['Category']['name']}->find('first', array(
			'fields'	=>	'alias',
			'conditions'	=>	array('id' => $this->configs['Category']['root_id'])
		));
		$this->EditablePage->Behaviors->detach('Translate');
		$records = $this->EditablePage->getAllPagesInCategory($info[$this->models['Category']['name']]['alias'], array('contain' => ''));
		foreach ($records as $record) {
			$data[$this->models['Page']['name']] = $record['EditablePage'];
			
			//$data[$this->models['Page']['name']]['id'] = null;
			$this->{$this->models['Page']['name']}->create($data);
			$this->{$this->models['Page']['name']}->save($data);
		}
		
	}
	function migrate_related_pages() {
		//exit;
		$this->{$this->models['Page']['name']}->Behaviors->attach('Containable');
		
		// Tìm tất cả các trang thuộc danh mục root_id
		$pages = $this->EditablePage->find('all', array(
			'conditions'	=>	array(
				'ReferenceCategory.category_id'	=>	$this->configs['Category']['root_id']
			),
			'fields'	=>	array('EditablePage.id','alias'),
			'order'		=>	array("EditablePage.priority asc"),
			'joins' => array(
				array(
					'table' => 'editable_pages_categories',
					'type' => 'INNER',
					'alias' => 'ReferenceCategory',
					'conditions' => array(
						"EditablePage.id = ReferenceCategory.page_id"
					)
				),
				array(
					'table' => 'categories',
					'type' => 'INNER',
					'alias' => 'Category',
					'conditions' => array(
						'Category.id = ReferenceCategory.category_id'
					)
				)
			),
			'contain'	=>	array('Category')
		));
		
		// Với mỗi trang, migrate nó
		foreach ($pages as $page) {
			$this->related_pages($page['EditablePage']['id']);
		}
	}
	
	function related_pages($id) {
		// Tìm thông tin related ứng với trang , sử dụng ID
		$options = array(
			'conditions'	=>	array(
				'RelatedReference.foreign_key'	=>	$id,
				'RelatedReference.model'		=>	'EditablePage',
				'RelatedReference.group'		=>	'Features'
			),
			'contain'		=>	array(),
			'fields'		=>	array(
				'RelatedReference.id', 'RelatedReference.id'
			),
			'order'			=>	"EditablePage.priority asc",
			'joins' => array(
				array(
					'table' => 'editable_pages_categories',
					'type' => 'INNER',
					'alias' => 'ReferenceCategory',
					'conditions' => array(
						"EditablePage.id = ReferenceCategory.page_id"
					)
				),
				array(
					'table' => 'related_pages',
					'type' => 'INNER',
					'alias' => 'RelatedReference',
					'conditions' => array(
						"EditablePage.id = RelatedReference.related_page_foreign_key",
						'RelatedReference.related_page_model'	=>	'EditablePage'
					)
				)
			),
			'limit'	=>	10000
		);

		$related_pages = $this->EditablePage->find('list', $options);
		//debug($related_pages);
		// Update Related Page Model
		$this->RelatedPage->updateAll(
			array(
				'related_page_model' => "'".$this->models['Page']['name']."'",
				'model' => "'".$this->models['Page']['name']."'"
			),
			array('id' => $related_pages)
		);
		
		// Lấy tất cả related_pages
		$e_ids = $this->RelatedPage->find('list', array(
			'fields'	=>	array('related_page_foreign_key', 'related_page_foreign_key'),
			'conditions'	=>	array('id' => $related_pages)
		));
		$this->EditablePage->Behaviors->detach('Translate');
		$records = $this->EditablePage->find('all', array(
			'conditions'	=>	array(
				'EditablePage.id'	=>	$e_ids
			)
		));
		$languages = Configure::read('Config.languages');
		foreach ($records as $record) {
			$data[$this->models['Page']['name']] = $record['EditablePage'];
			foreach ($languages as $langCode => $language) {
				$this->{$this->models['Page']['name']}->locale = $language;
				$this->{$this->models['Page']['name']}->create($data);
				$this->{$this->models['Page']['name']}->save($data);
			}
		}
	}
}
?>